package jsyf.oa.dao.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import jsyf.com.dao.BaseJdbcDao;
import jsyf.oa.entity.system.User2GroupEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

/**
 * Created by Administrator on 2018/1/1 0001.
 */
@Repository
public class User2GroupDao extends BaseDao {

    public User2GroupDao() {
        super.iBaseEntity = new User2GroupEntity();
    }

    public List<User2GroupEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<User2GroupEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            User2GroupEntity entity = (User2GroupEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(entity);
        }
        return entityList;
    }

    public void insertBatch(String userIds, String groupId){
        List<LinkedHashMap<String,Object>> user2GroupList = new ArrayList<LinkedHashMap<String,Object>>();
        LinkedHashMap<String,Object> user2GroupMap = null;
        List<String> list = BaseJdbcDao.stringToList(userIds);
        for (int i = 0; i < list.size(); i++) {
            user2GroupMap = new LinkedHashMap<>();
            user2GroupMap.put("ID",super.generateKey());
            user2GroupMap.put("USER_ID",list.get(i));
            user2GroupMap.put("GROUP_ID",groupId);
            user2GroupList.add(user2GroupMap);
        }
        super.insertBatch("T_SYS_USER_2_GROUP",user2GroupList);
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, User2GroupEntity.class);
    }
}

